//
//  XHRTopicVoiceView.m
//  BuDeJie
//
//  Created by haoran on 16/4/15.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHRTopicVoiceView.h"
#import "XHRTopicItem.h"
#import "XHRSeeBigPictureViewController.h"


@interface XHRTopicVoiceView()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *playcountLabel;
@property (weak, nonatomic) IBOutlet UILabel *voicetimeLabel;

@end
@implementation XHRTopicVoiceView


-(void)awakeFromNib{
    
    self.autoresizingMask = UIViewAutoresizingNone;
    self.imageView.userInteractionEnabled = YES;
    
    [self.imageView addGestureRecognizer: [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seeBigPicture)]];

}

/**
 *  查看大图
 */
-(void)seeBigPicture{
    XHRSeeBigPictureViewController * vc = [[XHRSeeBigPictureViewController alloc]init];
    vc.topicItem = self.topicItem;
    [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
    
}

-(void)setTopicItem:(XHRTopicItem *)topicItem{
    
    _topicItem = topicItem;
    // 设置背景图
//
//    UIImage * placeholderImage = [UIImage imageNamed:@"header_cry_icon"];
//    // 检查缓存
//    UIImage * originalImage = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:topicItem.image1];
//    
//    if (originalImage) { // 有沙盒缓存
//        [self.imageView sd_setImageWithURL:[NSURL URLWithString:topicItem.image1] placeholderImage:placeholderImage];
//        XHRLog(@"有缓存");
//    }else { // 没有沙盒缓存
//        AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
//        if (manager.isReachableViaWiFi) {// 如果是WiFi环境 下载原图
//            [self.imageView sd_setImageWithURL:[NSURL URLWithString:topicItem.image1] placeholderImage:placeholderImage];
//            XHRLog(@"wifi环境,下载大图");
//        }else if(manager.isReachableViaWWAN){// 如果是蜂窝数据
//            BOOL alwaysDownloadOriginalImage = [[NSUserDefaults standardUserDefaults]boolForKey:@"alwaysDownloadOriginalImage"];
//#warning 从沙盒中读取用户的配置项：在3G\4G环境是否仍然下载原图
//            if (alwaysDownloadOriginalImage) { // 下载原图
//                [self.imageView sd_setImageWithURL:[NSURL URLWithString:topicItem.image1] placeholderImage:placeholderImage];
//            }else{ // 下载小图
//                [self.imageView sd_setImageWithURL:[NSURL URLWithString:topicItem.image0] placeholderImage:placeholderImage];
//            }
//        }else{ // 没有网络 访问沙盒有没有小图缓存
//            XHRLog(@"没网络");
//            UIImage *thumbnailImage = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:topicItem.image0];
//            if (thumbnailImage) {
//                [self.imageView sd_setImageWithURL:[NSURL URLWithString:topicItem.image0] placeholderImage:placeholderImage];
//                XHRLog(@"有小图缓存");
//            }else{
//                XHRLog(@"无网络,无小图缓存,设置占位图");
//                [self.imageView sd_setImageWithURL:nil placeholderImage:placeholderImage];
//            }
//            
//        }
//        
//    }

    
    [self.imageView xhr_setImageWithOriginalImageURL:topicItem.image1 thumbnailImageURL:topicItem.image0 placeholderImage:nil completed:nil];
    
    
    // 设置播放次数
    if (topicItem.playcount > 10000) {
        self.playcountLabel.text = [NSString stringWithFormat:@"%.1f万播放",topicItem.playcount /10000.0];
    }else{
        self.playcountLabel.text = [NSString stringWithFormat:@"%zd播放",topicItem.playcount];
    }
    // 设置音频时间
    self.voicetimeLabel.text = [NSString stringWithFormat:@"%02zd:%02zd",topicItem.voicetime / 60, topicItem.voicetime % 60];
}


@end
